package com.zheng.business.web.service;

import com.zheng.business.common.constant.BusinessConstant;
import com.zheng.business.common.constant.BusinessResult;
import com.zheng.business.common.constant.BusinessResultConstant;
import com.zheng.business.dao.model.*;
import com.zheng.business.rpc.api.*;
import com.zheng.business.web.util.StatisticsUtil;
import com.zheng.common.constant.PayConstant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.ModuleConstant;
import com.zheng.common.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: BusinessOrderExpireHttpTaskService <br>
 * @Description: TODO <br>
 * @date 2019/8/8 17:29<br>
 **/
@Service
public class BusinessOrderExpireHttpTaskService {

    @Autowired
    BusinessOrderUmbrellaConsumptionService businessOrderUmbrellaConsumptionService;
    @Autowired
    BusinessOrderUmbrellaDepositService businessOrderUmbrellaDepositService;
    @Autowired
    BusinessOrderUmbrellaTimecardService businessOrderUmbrellaTimecardService;
    @Autowired
    BusinessOrderUmbrellaRechargeService businessOrderUmbrellaRechargeService;

    /**
     * 设置过期/自动取消订单。
     * @param dateStr  //处理日期,如2018-01-01
     * @param type  //300 租伞;301 预付金;302 充值;303 时长卡;
     * @return
     */
    public BusinessResult orderExpireByDate(String dateStr,String type) {
        if(StringUtils.isEmpty(dateStr)) {
            dateStr = DateUtil.getCurrentDate(Boolean.TRUE);
        }
        Date date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY_MM_DD);
        Date beginDate = DateUtil.getBeginTime(date);
        Date endDate = DateUtil.getEndTime(date);

        if(StringUtils.isEmpty(type)){
            //不区分类型，全部执行
            //租伞订单
            dealConsumptionOrderExpireByDate(beginDate,endDate);
            //预付金订单
            dealDepositOrderExpireByDate(beginDate,endDate);
            //充值订单
            dealRechargeOrderExpireByDate(beginDate,endDate);
            //时长卡订单
            dealTimecardOrderExpireByDate(beginDate,endDate);
        }else{
            if(type.equals(ModuleConstant.UMBRELLA_CONSUMPTION_BIZ_SEQUENCE_NO_PREFIX)){
                //租伞订单
                dealConsumptionOrderExpireByDate(beginDate,endDate);
            }else if(type.equals(ModuleConstant.UMBRELLA_DEPOSIT_BIZ_SEQUENCE_NO_PREFIX)){
                //预付金订单
                dealDepositOrderExpireByDate(beginDate,endDate);
            }else if(type.equals(ModuleConstant.UMBRELLA_RECHARGE_BIZ_SEQUENCE_NO_PREFIX)){
                //充值订单
                dealRechargeOrderExpireByDate(beginDate,endDate);
            }else if(type.equals(ModuleConstant.UMBRELLA_TIMECARD_BIZ_SEQUENCE_NO_PREFIX)){
                //时长卡订单
                dealTimecardOrderExpireByDate(beginDate,endDate);
            }
        }
        return new BusinessResult(BusinessResultConstant.SUCCESS,null);
    }

    /**
     * 更新租伞订单为失效
     * @param beginDate
     * @param endDate
     */
    private void dealConsumptionOrderExpireByDate(Date beginDate,Date endDate) {
        //更新条件
        BusinessOrderUmbrellaConsumptionExample updateExample = new BusinessOrderUmbrellaConsumptionExample();
        BusinessOrderUmbrellaConsumptionExample.Criteria updateCriteria = updateExample.createCriteria();
        updateCriteria.andCreatetimeBetween(beginDate,endDate);
        updateCriteria.andStatusEqualTo(BusinessConstant.UM_CONSUMPTION_ORDER_STATUS_0);
        //更新为自动取消状态
        BusinessOrderUmbrellaConsumption updateOrder = new BusinessOrderUmbrellaConsumption();
        updateOrder.setStatus(BusinessConstant.UM_CONSUMPTION_ORDER_STATUS_2);
        businessOrderUmbrellaConsumptionService.updateByExampleSelective(updateOrder, updateExample);
    }
    /**
     * 更新时长卡订单为失效
     * @param beginDate
     * @param endDate
     */
    private void dealTimecardOrderExpireByDate(Date beginDate,Date endDate) {
        //更新条件
        BusinessOrderUmbrellaTimecardExample updateExample = new BusinessOrderUmbrellaTimecardExample();
        BusinessOrderUmbrellaTimecardExample.Criteria updateCriteria = updateExample.createCriteria();
        updateCriteria.andExpiretimeBetween(beginDate,endDate);
        updateCriteria.andStatusEqualTo(PayConstant.PAY_STATUS_INIT & 0xFF);
        //更新为过期状态
        BusinessOrderUmbrellaTimecard updateTimecard = new BusinessOrderUmbrellaTimecard();
        updateTimecard.setStatus(PayConstant.PAY_STATUS_EXPIRED & 0xFF);
        businessOrderUmbrellaTimecardService.updateByExampleSelective(updateTimecard, updateExample);
    }
    /**
     * 更新预付金订单为失效
     * @param beginDate
     * @param endDate
     */
    private void dealDepositOrderExpireByDate(Date beginDate,Date endDate) {
        //更新条件
        BusinessOrderUmbrellaDepositExample updateExample = new BusinessOrderUmbrellaDepositExample();
        BusinessOrderUmbrellaDepositExample.Criteria updateCriteria = updateExample.createCriteria();
        updateCriteria.andExpiretimeBetween(beginDate,endDate);
        updateCriteria.andStatusEqualTo(PayConstant.PAY_STATUS_INIT & 0xFF);
        //更新为过期状态
        BusinessOrderUmbrellaDeposit updateOrder = new BusinessOrderUmbrellaDeposit();
        updateOrder.setStatus(PayConstant.PAY_STATUS_EXPIRED & 0xFF);
        businessOrderUmbrellaDepositService.updateByExampleSelective(updateOrder, updateExample);
    }
    /**
     * 更新充值订单为失效
     * @param beginDate
     * @param endDate
     */
    private void dealRechargeOrderExpireByDate(Date beginDate,Date endDate) {
        //更新条件
        BusinessOrderUmbrellaRechargeExample updateExample = new BusinessOrderUmbrellaRechargeExample();
        BusinessOrderUmbrellaRechargeExample.Criteria updateCriteria = updateExample.createCriteria();
        updateCriteria.andExpiretimeBetween(beginDate,endDate);
        updateCriteria.andStatusEqualTo(PayConstant.PAY_STATUS_INIT & 0xFF);
        //更新为过期状态
        BusinessOrderUmbrellaRecharge updateOrder = new BusinessOrderUmbrellaRecharge();
        updateOrder.setStatus(PayConstant.PAY_STATUS_EXPIRED & 0xFF);
        businessOrderUmbrellaRechargeService.updateByExampleSelective(updateOrder, updateExample);
    }

}
